package com.fzj.charging.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfiguration {
    private static final String QUEUE_NAME = "charging_order_queue";
    public static final String EXCHANGE_NAME = "charging_order_exchange";
    public static final String ROUTING_KEY = "charging_order_routingKey";

    public static final String DEAD_LETTER_QUEUE_NAME = "dead_charging_order_queue";
    private static final String DEAD_LETTER_EXCHANGE_NAME = "dead_charging_order_exchange";
    private static final String DEAD_LETTER_ROUTING_KEY = "dead_charging_order_routingKey";

    @Bean
    public Queue queue(){
        Map<String,Object> args = new HashMap<>();
        //模拟时间 设置消息TTL(生存时间)为1分钟
        Integer ttl = 60*1000;
        args.put("x-message-ttl",ttl);
        //设置正常队列的死信队列的交换机exchange
        args.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE_NAME);
        //设置正常队列的死信队列的routingKey
        args.put("x-dead-letter-routing-key",DEAD_LETTER_ROUTING_KEY);
        //消息持久化
        Boolean durable = true;
        //连接级别的互斥
        Boolean exclusive = false;
        //队列是否删除
        Boolean autoDelete = false;
        return new Queue(QUEUE_NAME,durable,exclusive,autoDelete,args);

    }
    @Bean
    public DirectExchange exchange(){

        //消息持久化
        Boolean durable = true;
        //是否删除
        Boolean autoDelete = false;
        return new DirectExchange(EXCHANGE_NAME,durable,autoDelete);
    }

    @Bean
    public Binding orderBing(){
     return BindingBuilder.bind(queue()).to(exchange()).with(ROUTING_KEY);
    }

    @Bean
    public Queue deadQueue(){
        //消息持久化
        Boolean durable = true;
        //是否删除
        Boolean autoDelete = false;
        return new Queue(DEAD_LETTER_QUEUE_NAME,durable,false,autoDelete);
    }
    @Bean
    public DirectExchange deadExchange(){
        //消息持久化
        Boolean durable = true;
        //是否删除
        Boolean autoDelete = false;
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME,durable,autoDelete);
    }

    @Bean
    public Binding deadOrderBing(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(DEAD_LETTER_ROUTING_KEY);
    }
}
